<template>
    <el-card>
        <div slot="header" class="clearfix">
            <h4>发布|下线申请详情</h4>
        </div>
        <div style="margin-bottom: 10px">操作以下接口：</div>
        <el-table :data="tableData" size="mini" border style="width: 100%">
            <el-table-column label="所属服务" prop="serviceName"></el-table-column>
            <el-table-column label="接口名" prop="configName"></el-table-column>
            <el-table-column label="状态" prop="currentState" width="100"></el-table-column>
        </el-table>
        <el-form :model="form" label-width="120px" disabled style="margin-top: 10px">
          <el-form-item label="标题：">
              <el-input v-model="form.title" size="small"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="12">
                <el-form-item label="申请人：">
                    <el-input v-model="form.createBy" size="small"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="审核人：">
                    <el-input v-model="form.approvalBy" size="small"></el-input>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="申请类型：">
                    <el-input v-model="form.type" size="small"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="审核结果：">
                    <el-input v-model="form.approvalResult" size="small"></el-input>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="申请原因：">
                    <el-input v-model="form.reason" size="small"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="审核备注：">
                    <el-input v-model="form.approvalRemark" size="small"></el-input>
                </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
                <el-form-item label="申请时间：">
                    <el-input v-model="form.createTime" size="small"></el-input>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="审核时间：">
                    <el-input v-model="form.approvalTime" size="small"></el-input>
                </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <el-divider>操作日志</el-divider>
         <el-table :data="tableData2" size="mini" border style="width: 100%">
            <el-table-column label="操作类型" prop="opType" width="100"></el-table-column>
            <el-table-column label="所属服务" prop="name"></el-table-column>
            <el-table-column label="接口名" prop="opBy"></el-table-column>
            <el-table-column label="操作人" prop="opBy"></el-table-column>
            <el-table-column label="操作时间" prop="opTime"></el-table-column>
            <el-table-column label="备注" prop="remark"></el-table-column>
        </el-table>
        <el-row>
          <el-col :span="24">
              <div style="margin-top: 20px;text-align: center">
                  <el-button type="primary" plain size="small" @click="handleEditUser">修改审核人</el-button>
                  <el-button type="primary" size="small" @click="handleRevoke">撤回</el-button>
                  <el-button plain size="small" @click="goBack">返回</el-button>
              </div>
          </el-col>
        </el-row>
        <el-dialog title="修改审核人" :visible.sync="dialogVisible" width="30%">
            <el-form :model="editForm" label-width="100px">
              <el-form-item label="审核人" prop="approvalUserId">
                  <el-select v-model="editForm.approvalUserId" placeholder="请选择" size="small">
                    <el-option v-for="item in applyUserList" :label="item.userName" :key="item.userId" :value="item.userId"></el-option>
                  </el-select>
              </el-form-item>
            </el-form>
             <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false" size="small">取 消</el-button>
                <el-button type="primary" @click="handleSubmit" size="small">确 定</el-button>
             </span>
        </el-dialog>
    </el-card>
</template>

<script>
import { getApplyConfigList,getApplyDetail,getApplyUser,changeApprovalUser,revoke } from '@/api/aggregate/apply.api.js'
    export default {
        data() {
            return {
                form: {},
                tableData: [],
                tableData2: [],
                dialogVisible: false,
                editForm: {},
                applyUserList: [],
                configIds: []
            }
        },
        mounted() {
            this.getDetail();
        },
        methods: {
            async getDetail() {
                 const originId = this.$route.query.originId;
                 const result = await getApplyDetail(originId);
                 if(result.code === 200) {
                    this.form = result.data;
                    this.tableData = result.data.configs;
                    this.configIds = result.data.configs.map(item=>item.configId)
                 }
                 const result2 = await getApplyConfigList(originId);
                 if(result2.code === 200) {
                    this.tableData2 = result2.data;
                 }
            },
            goBack() {
                this.$router.go(-1);
            },
           async handleEditUser() {
                const result = await getApplyUser({configIds: this.configIds});
                if(result.code===200) {
                    this.applyUserList = result.data.approvalUsers;
                    this.dialogVisible = true;
                }
            },
            async handleSubmit() {
                const data = {
                    applyId: this.$route.query.originId,
                    approvalUserId: this.editForm.approvalUserId
                }
               const result = await changeApprovalUser(data);
               if(result.code === 200) {
                   this.$message.success('修改成功');
                   this.getDetail();
                   this.dialogVisible = false;
               }
            },
            handleRevoke() {
                this.$confirm('确定撤回该申请?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                         const originId = this.$route.query.originId;
                        const result = await revoke(originId);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '撤回成功!'
                           });
                           this.getDetail();
                        }
                    }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消撤回'
                    });          
                });
            }
        }
    }
</script>

<style lang="scss" scoped>

</style>